"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mysql_1 = __importDefault(require("mysql"));
const Logger_1 = __importDefault(require("../utils/Logger"));
class MySQLService {
    constructor() {
        this.connection = mysql_1.default.createConnection({
            host: "175.27.187.8",
            user: 'root',
            password: 'ZuoHui00.',
            database: 'yeqi',
            port: 3306,
            autoReconnect: true //永久连接
        });
        this.connection.connect();
        //定时查询保持活性
        setInterval(() => { this.connection.query('SELECT 1'); }, 5000);
    }
    /** 初始化数据库 */
    init() {
        this.initUser();
    }
    /** 将玩家登录状态归0 */
    /** 将玩家所在房间归-1 */
    initUser() {
        var sql = 'update user set is_logining = ?, room_id = ?';
        let datas = [0, 0];
        // 执行sql,并处理执行的结果
        this.connection.query(sql, datas, function (err, result) {
            if (err) {
                Logger_1.default.log('初始化失败： ', err.message);
                return;
            }
            Logger_1.default.log('初始化成功,受影响的行数：', result.affectedRows);
        });
    }
    /** 查询用户 */
    selectByUsername(username, callback) {
        this.connection.query('select * from user where username = ? limit 1', [username], function (err, result) {
            callback(result);
        });
    }
    /** 注册用户 */
    insertUser(username, password) {
        //username,password,is_logining,room_id,score,groly,number_of_matches,number_of_wins
        let sql = "insert into user values( ? , ? , 0 , 0 , 0 , 0 , 0 , 0)";
        let datas = [username, password];
        this.connection.query(sql, datas, function (err, result) {
            if (err) {
                Logger_1.default.log('插入失败', err.message);
                return;
            }
            Logger_1.default.log('注册成功：' + username);
        });
    }
    /** 修改登录状态 */
    updateUserLoginState(username, state) {
        let sql = 'update user set is_logining = ? where username = ?';
        let datas = [state, username];
        // 执行sql,并处理执行的结果
        this.connection.query(sql, datas, function (err, result) {
            if (err) {
                Logger_1.default.log('登录状态修改失败： ', err.message);
                return;
            }
            Logger_1.default.log('登录状态修改，玩家： ' + username + " state: " + state);
        });
    }
    /** 修改所在房间号 */
    updateUserRoomId(username, room_id) {
        let sql = 'update user set room_id = ? where username = ?';
        let datas = [room_id, username];
        // 执行sql,并处理执行的结果
        this.connection.query(sql, datas, function (err, result) {
            if (err) {
                Logger_1.default.log('修改所在房间号 ', err.message);
                return;
            }
            Logger_1.default.log('修改所在房间号，玩家：' + username + " room_id: " + room_id);
        });
    }
    /** 修改玩家分数 */
    updateUserScore(username, score) {
        let sql = 'update user set score = ? where username = ?';
        let datas = [score, username];
        // 执行sql,并处理执行的结果
        this.connection.query(sql, datas, function (err, result) {
            if (err) {
                Logger_1.default.log('修改玩家分数 ', err.message);
                return;
            }
            Logger_1.default.log('修改玩家分数: ' + username + " room_id: " + score);
        });
    }
    /** 排行榜 */
    selectRankingList(callback) {
        let sql = 'select username,score from user order by score desc limit 15';
        this.connection.query(sql, [], function (err, result) {
            if (err) {
                Logger_1.default.log('获取排行榜失败： ', err.message);
            }
            else {
                callback(result);
            }
        });
    }
}
exports.default = new MySQLService();
